package com.kasax.android.arch.base.app;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.kasax.android.arch.manager.AppActivityManager;
import com.kasax.android.arch.utils.DefActLifecycle;


/**
 * App初始化配置桥
 */
public class AppInitBradge {
    private Application mApp;
    protected static Context mContext;
    protected static int mThreadId;
    protected static String mThreadName;
    protected static Handler handler;
    protected static Looper looper;
    protected static int myPid;
    private String logTag;
    private boolean isDebug;

    private AppInitBradge() {

    }

    public static AppInitBradge getInstance() {
        return Singleton.INSTANCE;
    }

    private static class Singleton {
        private static final AppInitBradge INSTANCE = new AppInitBradge();
    }

    public void doInit(Application application, String logTag, boolean isDebug) {
        this.mApp = application;
        this.logTag = logTag;
        this.isDebug = isDebug;
        initGlobalParams();
    }

    private void initGlobalParams() {
        mContext = mApp.getApplicationContext();
        handler = new Handler();
        looper = mApp.getMainLooper();
        myPid = android.os.Process.myPid();
        mThreadId = android.os.Process.myTid();
        mThreadName = Thread.currentThread().getName();
        mApp.registerActivityLifecycleCallbacks(new DefActLifecycle() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                super.onActivityCreated(activity, savedInstanceState);
                //全局强制竖屏
                activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                AppActivityManager.getInstance().addActivity(activity);
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                super.onActivityDestroyed(activity);
                AppActivityManager.getInstance().removeActivity(activity);
            }
        });
    }

    /**
     * 获取当前正在显示的activity 仅仅是获取不从任务栈中移除
     */
    public Activity getCurrentAct() {
        return AppActivityManager.getInstance().getTopActivity();
    }

    public String getMainThreadName() {
        return mThreadName;
    }

    public int getAppProcessId() {
        return myPid;
    }

    public int getMainThreadId() {
        return mThreadId;
    }

    public Looper getAppLooper() {
        return looper;
    }

    public Handler getAppHandler() {
        return handler;
    }

    public Context getContext() {
        return mContext;
    }


}
